﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;

namespace WpfUsercontrol.UserControls.Permissions
{
    public static class PermissionManager
    {
        // 当前用户权限缓存
        private static HashSet<string> _currentUserPermissions = new HashSet<string>() { "admin" };

        // 注册权限附加属性
        public static readonly DependencyProperty RequiredPermissionProperty =
            DependencyProperty.RegisterAttached(
                "RequiredPermission",
                typeof(string),
                typeof(PermissionManager),
                new PropertyMetadata(string.Empty, OnRequiredPermissionChanged));

        public static string GetRequiredPermission(DependencyObject obj)
        {
            return (string)obj.GetValue(RequiredPermissionProperty);
        }

        public static void SetRequiredPermission(DependencyObject obj, string value)
        {
            obj.SetValue(RequiredPermissionProperty, value);
        }

        private static void OnRequiredPermissionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UIElement element)
            {
                UpdateElementVisibility(element);
            }
        }

        // 更新UI元素可见性
        private static void UpdateElementVisibility(UIElement element)
        {
            var requiredPermission = GetRequiredPermission(element);

            if (string.IsNullOrEmpty(requiredPermission))
            {
                element.Visibility = Visibility.Visible;
                return;
            }

            element.Visibility = _currentUserPermissions.Contains(requiredPermission)
                ? Visibility.Visible
                : Visibility.Collapsed;
        }

        // 设置当前用户权限
        public static void SetUserPermissions(IEnumerable<string> permissions)
        {
            _currentUserPermissions.Clear();

            if (permissions != null)
            {
                foreach (var permission in permissions)
                {
                    _currentUserPermissions.Add(permission);
                }
            }

            // 更新所有绑定了权限的UI元素
            UpdateAllElements();
        }

        // 更新所有UI元素
        private static void UpdateAllElements()
        {
            // 在实际应用中，你需要遍历所有窗口和控件
            // 这里简化处理，只处理主窗口
            if (Application.Current.MainWindow != null)
            {
                UpdateChildren(Application.Current.MainWindow);
            }
        }

        private static void UpdateChildren(DependencyObject parent)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is UIElement element)
                {
                    UpdateElementVisibility(element);
                }
                UpdateChildren(child);
            }
        }

        // 检查权限
        public static bool HasPermission(string permission)
        {
            return _currentUserPermissions.Contains(permission);
        }
    }
}
